<?php
require_once 'class.wp_environment.php';

class ImportRSS extends WpEnvironemt{

private $parentPage = null;
private $rssInsertPage = null;

public function __construct($parentPage, $rssInsertPage)
{
	$this->parentPage = $parentPage;
	$this->rssInsertPage = $rssInsertPage;
}

protected function initAllVariables() {}

public function processRSS()
{
	if( isset($_GET['post']) )
		$this->processRSSasNewWordpressPost();
	else	
		$this->processRSSasNewWordpressPost_UI();
}
private function processRSSasNewWordpressPost()
{
	try{
		global $current_user;
		get_currentuserinfo();

		if( isset($_POST['title']) || isset($_POST['content']))
		{
			//throw new Exception('Some parameter(s) is missing');
			if( isset($_POST['tags_input']) )
				$tags_input = explode(';', $_POST['tags_input']);
			else if (isset($_POST['link']))
				$tags_input = parse_url($_POST['link'], PHP_URL_HOST);
			else
				$tags_input = array('Imported via RSS');

			$my_post = array(
				'post_title' => $_POST['title'],
				'post_content' => $_POST['content'],
				'post_status' => 'pending',
				'post_author' => $current_user->ID,
				'tags_input' => $tags_input
			);
			
			$post_id = wp_insert_post( $my_post, true );
			if( is_wp_error($post_id) )
			{ 
				throw new Exception($post_id->get_error_message());
			}
			echo "Successfully queued and having pending status: ". get_bloginfo('siteurl'). '/?p='. $post_id;
		}
		else
			throw new Exception('Somehow post title or post content were not sent to server. Please try again.');
	}catch(Exception $e)
	{
		$this->printException($e);
	}
	exit(); //This was an ajax request.
}

private function processRSSasNewWordpressPost_UI()
{
?>
<h1> Import your post via rss feed </h1>
Insert RSS link:
<input type="text" value="" size="50" maxlength="1000" id="my_rss" onclick='select();' />
<button class="click" ID="importRSS">Import</button>
<br/><small>Example: <code>https://www.facebook.com/feeds/page.php?format=rss20&id=241990755952681</code></small>

<div  style='width:600px;'>
<br/><br/><br/><span style='color:red'></span><br/>
<table id="data_table" border="1">
<thead>
<tr>
<th>Serial</th>
<th>Title of the post</th>
<th>Import link</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>

<script type='text/javascript' src='//cdn.datatables.net/1.10.0/js/jquery.dataTables.js'></script>
<style type="text/css">@import url("//cdn.datatables.net/1.10.0/css/jquery.dataTables.css");</style>
<style type="text/css">td { max-width: 100px; }</style>
<script type="text/javascript">

(function($){
$('#data_table').dataTable({'aaSorting': [[ 0, 'desc' ]]} );

$("#importRSS").click(function(){
	var url=$("#my_rss").val();
	//var url="http://api.flickr.com/services/feeds/groups_pool.gne?id=741815@N22&lang=en-us";
	$.getJSON("https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=50&callback=?&q="+encodeURIComponent(url),
	function(data)
	{
		// importedData = a global variable; not good
		if( data.responseStatus == 400)
		{
			alert("Error: " + data.responseDetails);
			return;
		}
		importedData= data.responseData.feed.entries;
		for(var i=0; i<data.responseData.feed.entries.length;++i)
		{
			if( data.responseData.feed.entries[i].content == '')
				continue;
				
			$('#data_table').dataTable().fnAddData([i, data.responseData.feed.entries[i].title, '<button onclick="insertPost('+i+')" style="font-style:bold" >Submit</button>']);
		}
	});

	insertPost= function(i)
	{
		var data={
			title: importedData[i].title,
			content: importedData[i].content,
			link: importedData[i].link
		};

		$.post("<?PHP echo get_bloginfo('wpurl') . "/wp-admin/admin-ajax.php?action={$this->rssInsertPage}&post=post2"; ?>", data,
		function(response)
		{
			//console.log(response);
			alert(response);
			$('#data_table').dataTable().fnDeleteRow(i);
		});
	}
});

//end of function
})(jQuery);
</script>

<br/><br/><br/><br/>

<?php 
}

} // class ends here
?>